//package aimis.config.db;
//
//import aimis.domain.Department;
//import aimis.domain.Role;
//import aimis.domain.User;
//import aimis.repo.AuthorityRepo;
//import aimis.repo.DepartmentRepo;
//import aimis.repo.RoleRepo;
//import aimis.repo.UserRepo;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.context.annotation.Profile;
//import org.springframework.security.crypto.password.PasswordEncoder;
//
//@Profile(value = {"dev", "local", "prod"})
//@Configuration
//public class AdminInitConfig {
//
//    private Logger LOGGER = LoggerFactory.getLogger(AdminInitConfig.class);
//    @Autowired
//    private AuthorityRepo authorityRepo;
//    @Autowired
//    private RoleRepo roleRepo;
//    @Autowired
//    private DepartmentRepo departmentRepo;
//    @Autowired
//    private UserRepo userRepo;
//    @Value("${admin.department}")
//    private String adminDepartment;
//    @Value("${admin.role}")
//    private String adminRole;
//    //    @Value("${admin.authority}")
////    private String adminAuthority;
//    @Value("${admin.name}")
//    private String adminName;
//    @Value("${admin.password}")
//    private String adminPassword;
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//
//    @Bean
//    @DependsOn("authorityInit")
//    public void adminInit() {
//        try {
//            LOGGER.info("初始化超级管理员开始...");
////            Authority authority = new Authority();
////            authority.setSort(0);
////            authority.setName(adminAuthority);
////            authority.setDescription("超级管理员权限");
////            authorityRepo.save(authority);
//            Role role = roleRepo.findByName(adminRole);
//            if (role!=null){
//                var authoritySet = authorityRepo.findAll();
//                role.getAuthorities().clear();
//                role.getAuthorities().addAll(authoritySet);
//                roleRepo.save(role);
//            }else{
//                Department department = newDepartment(adminDepartment,null);
//                department.setAccessAllDepartment(true);
//                departmentRepo.save(department);
//
//                role = new Role();
//                role.setName(adminRole);
//                role.setDescription("管理员");
//
//    //            var authoritySet = authorityRepo.findByNameLike(adminAuthority);
//    //            authoritySet.addAll(authorityRepo.findByNameLike("参数配置"));
//                var authoritySet = authorityRepo.findAll();
//                role.getAuthorities().addAll(authoritySet);
//                roleRepo.save(role);
//
//                User user = new User();
//                user.setName(adminName);
//                user.setLoginName(adminName);
//                user.setDepartment(department);
//                user.getRoles().add(role);
//                user.setPassword(passwordEncoder.encode(adminPassword));
//                userRepo.save(user);
//            }
//            LOGGER.info("初始化超级管理员结束...");
//        } catch (Exception e) {
//            LOGGER.info("初始化超级管理员失败...");
//            e.printStackTrace();
//        }
//
//    }
//
//    private Department newDepartment(String name, Department parent) {
//        try {
//            var department=departmentRepo.findByNameAndParent(name,parent);
//            if(department !=null)
//                return department; //已经存在则不添加
//            department = new Department();
//            department.setName(name);
//            department.setParent(parent);
//            department = departmentRepo.save(department);
//            if(department.getParent()==null)
//            {
//                department.setRoot(department);
//                department = departmentRepo.save(department);
//            }else{
//                department.setRoot(department.getParent().getRoot());
//                department = departmentRepo.save(department);
//            }
//            return department;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//
//
//
//}
